package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Dictionaries;
import cn.wolfcode.crm.domain.DictionariesItem;
import cn.wolfcode.crm.mapper.DictionariesItemMapper;
import cn.wolfcode.crm.mapper.DictionariesMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IDictionariesService;
import cn.wolfcode.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class DictionariesServiceImpl implements IDictionariesService {
    @Autowired
    private DictionariesMapper dictionariesMapper;

    @Autowired
    private DictionariesItemMapper dictionariesItemMapper;


    public void saveOrUpdate(Dictionaries entity) {
        if (entity.getId() == null) {
            dictionariesMapper.insert(entity);
        } else {
            dictionariesMapper.updateByPrimaryKey(entity);
        }
    }

    public void delete(Long id) {
        //先删除明细表的数据
        dictionariesItemMapper.deleteByDictionariesId(id);

        //然后删除主表数据
        dictionariesMapper.deleteByPrimaryKey(id);
    }

    public Dictionaries get(Long id) {
        return dictionariesMapper.selectByPrimaryKey(id);
    }

    public List<Dictionaries> listAll() {
        return dictionariesMapper.selectAll();
    }

    @Override
    public PageResult query(QueryObject qo) {
        int count = dictionariesMapper.queryCount(qo);
        if (count == 0) {
            return new PageResult(0, Collections.emptyList());
        }
        List<Dictionaries> dictionariess = dictionariesMapper.queryList(qo);
        return new PageResult(count, dictionariess);
    }

    @Override
    public List<DictionariesItem> selectItemName(String str) {
        return dictionariesMapper.selectItemName(str);
    }

}
